package vip

import (
	"strconv"

	"github.com/gogf/gf/v2/net/ghttp"

	bmodel "github.com/beherochuling/beesoft_api/app/model"
	model "github.com/beherochuling/beesoft_api/app/model/vip"
	service "github.com/beherochuling/beesoft_api/app/service/curd/vip"

	"github.com/beherochuling/beesoft_api/app/helper/net"
	"github.com/beherochuling/beesoft_api/app/service/util"
)

var Constant = constant{}

type constant struct{}

func (constant) List(r *ghttp.Request) { // 列表
	var req *model.ConstantListRequest
	util.ParseRequest(r, &req)
	res := net.NewResponse(r)

	result, total := service.Constant.List(r.Context(), req)
	// if result == nil {
	// 	res.ResponseException("1")
	// }

	res.SetHeader(bmodel.HeaderTotal, strconv.Itoa(total)).
		ResponseOk(result)
}
func (constant) Show(r *ghttp.Request) { // 详情
	id := r.Get("id").Uint()
	if id == 0 {
		panic("id is required")
	}

	result := service.Constant.Show(r.Context(), id)
	if result == nil {
		return
	}

	net.NewResponse(r).ResponseOk(result)
}
func (constant) Store(r *ghttp.Request) { // 创建
	var req *model.ConstantStoreRequest
	util.ParseRequest(r, &req)

	err := service.Constant.Store(r.Context(), req)
	if err != nil {
		return
	}

	net.NewResponse(r).ResponseOk()
}
func (constant) Update(r *ghttp.Request) { // 更新
	var req *model.ConstantUpdateRequest
	util.ParseRequest(r, &req)

	err := service.Constant.UpdateById(r.Context(), req.Id, req)
	if err != nil {
		return
	}

	net.NewResponse(r).ResponseOk()
}
func (constant) Destroy(r *ghttp.Request) { // 删除
	id := r.Get("id").Uint()
	if id == 0 {
		panic("id is required")
	}

	err := service.Constant.DestroyById(r.Context(), id)
	if err != nil {
		return
	}

	net.NewResponse(r).ResponseOk()
}
